package com.plumelog.server.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import com.plumelog.core.client.AbstractClient;
import com.plumelog.core.constant.LogMessageConstant;
import com.plumelog.core.dto.RunLogCompressMessage;
import com.plumelog.core.util.GfJsonUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Objects;

/**
 * @author xjwwiz
 */
@RestController
@CrossOrigin
public class HttpCollectController {
    private final Logger logger = LoggerFactory.getLogger(CollectController.class);

    @Autowired(required = false)
    private AbstractClient redisQueueClient;

    @RequestMapping({"/plumelogServer/compress/runLog"})
    public Result runLog(@RequestBody List<String> compressMessages) {
        Result result = new Result();
        try {
            for (int i = 0; i < compressMessages.size(); i++) {
                List<String> messages = compressMessages(compressMessages.get(i));
                if (Objects.isNull(messages)) {
                    continue;
                }
                redisQueueClient.putMessageList(LogMessageConstant.LOG_KEY_COMPRESS, messages);
            }
            result.setCode(200);
            result.setMessage("send logs! success");
            return result;
        } catch (Exception e) {
            result.setCode(500);
            result.setMessage("send runLog error! :" + e.getMessage());
            logger.error(result.getMessage(), e);
            return result;
        }
    }

    @RequestMapping({"/plumelogServer/compress/traceLog"})
    public Result traceLog(@RequestBody List<String> compressMessages) {
        Result result = new Result();
        try {
            for (int i = 0; i < compressMessages.size(); i++) {
                List<String> messages = compressMessages(compressMessages.get(i));
                if (Objects.isNull(messages)) {
                    continue;
                }
                redisQueueClient.putMessageList(LogMessageConstant.LOG_KEY_TRACE_COMPRESS, messages);
            }
            result.setCode(200);
            result.setMessage("send logs! success");
            return result;
        } catch (Exception e) {
            result.setCode(500);
            result.setMessage("send traceLog error! :" + e.getMessage());
            logger.error(result.getMessage(), e);
            return result;
        }
    }

    private List<String> compressMessages(String text) {
        RunLogCompressMessage message = JSON.parseObject(text, RunLogCompressMessage.class);

        if (Objects.isNull(message.getLength()) || message.getBody() == null) {
            return null;
        }
        return Lists.newArrayList(GfJsonUtil.toJSONString(message));
    }
}
